<template>
  <el-container class="con">
    <el-header class="head">
      <!-- 页面中添加布局元素 -->
      <div>
        <img src="../assets/1.png" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="exit" class="btn">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '44px':'200px'" class="aside">
        <div class="down" @click="toggleCol">|||</div>
        <el-menu
          :default-active="active"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#67C23A"
          :collapse="isCollapse"
          :collapse-transition="true"
          :router="true"
        >
          <el-submenu :index="'/'+item.path " v-for="item in menuList" :key="item.id">
            <template slot="title" class="icon">
              <i :class="'iconfont  icon '+iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              @click="activeFun('/'+item.path)"
              :index="'/'+item.path"
              v-for="item in item.children"
              :key="item.id"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="main">
        <!-- 占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuList: [],
      iconObj: {
        125: 'icon-users',
        103: 'icon-tijikongjian',
        101: 'icon-shangpin',
        102: 'icon-danju',
        145: 'icon-baobiao'
      },
      // 菜单列表选中值
      active: ''
    }
  },
  created() {
    this.getMenuList()
    this.active = window.sessionStorage.getItem('active')
  },
  methods: {
    // 点击将active赋值
    activeFun(val) {
      // console.log(val)
      window.sessionStorage.setItem('active', val)
      this.active = window.sessionStorage.getItem('active')
    },
    // 退出功能
    exit: function() {
      // 清空sessionstorg
      window.sessionStorage.clear()
      // 重定向到登录页面
      this.$router.push('/login')
    },
    // 获取用户列表
    async getMenuList() {
      var list = await this.$http.get('menus')
      if (list.status !== 200) {
        return this.$message.error('获取列表菜单失败')
      }
      // 将数据放到列表中
      this.menuList = list.data.data
      // console.log(this.menuList)
    },
    // 切换列表栏的开合
    toggleCol() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.con {
  height: 100%;
}
.head {
  background-color: black;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .btn {
  }
  div {
    margin-left: 100px;
    height: 100%;
    img {
      box-sizing: border-box;
      height: 100%;
      border-radius: 50%;
      padding: 5px;
      vertical-align: middle;
    }
    span {
      margin-left: 15px;
      color: wheat;
      font-size: 20px;
    }
  }
}
.aside {
  background-color: #525a62;
  .down {
    height: 30px;
    text-align: center;
    line-height: 30px;
    background-color: #4a5064;
    cursor: pointer;
    color: white;
  }
  .el-menu {
    border-right: none;
  }
}
.main {
  background-color: #dcdfe6;
}

.icon {
  margin-right: 10px;
}
</style>
